package server

import (
	"context"
	"fmt"
	"math/rand"
	"short-video/pkg"
	"short-video/short-video-rpc/initiate/mysql"
	"short-video/short-video-rpc/initiate/redis"
	"short-video/short-video-rpc/proto/video"
	"time"
)

func UserLogin(in *video.UserLoginReq) (*video.UserLoginResp, error) {
	user := mysql.VideoUsers{}
	err := user.GetUserByMoble(in.Mobile)
	if err != nil {
		fmt.Println("未查询到当前的手机号码")
		return nil, err
	}
	if user.Id == 0 {
		fmt.Println("当前的手机号未注册")
		err = user.CreateUserByMoble(in.Mobile)
		if err != nil {
			fmt.Println("用户自注册失败")
			return nil, err
		}
	}
	token, err := pkg.NewJWT("2211a").CreateToken(pkg.CustomClaims{ID: uint(user.Id)})
	return &video.UserLoginResp{Greet: token}, nil
}

func SendSms(in *video.SendSmseq) (*video.SendSmsResp, error) {
	redis.RDB.Get(context.Background(), "Sms"+in.Source+in.Mobile)
	code := rand.Intn(9000) + 1000
	redis.RDB.Set(context.Background(), "Sms"+in.Source+in.Mobile, code, 5*time.Minute)
	return &video.SendSmsResp{Greet: "验证码发送成功"}, nil
}

func UserFind(in *video.UserFindReq) (*video.UserFindResp, error) {
	user := mysql.VideoUsers{}
	li, err := user.GetUserById(in.Id)
	if err != nil {
		fmt.Println("未查询到此用户")
		return nil, err
	}
	var list []*video.UserItem
	for _, v := range li {
		lists := &video.UserItem{
			Name:          v.Name,
			NickName:      v.NickName,
			UserCode:      v.UserCode,
			Signature:     v.Signature,
			Sex:           v.Sex,
			Constellation: v.Constellation,
			AttendCount:   v.AttendCount,
			FansCount:     v.FansCount,
			ZanCount:      v.ZanCount,
			Status:        v.Status,
			AvatorFileId:  int64(v.AvatorFileId),
		}
		list = append(list, lists)
	}
	return &video.UserFindResp{List: list}, nil
}

func VideoUpload(in *video.VideoUploadReq) (*video.VideoUploadResp, error) {
	works := mysql.VideoWorks{}
	err := works.GetWorksById(in.Id)
	if err != nil {
		fmt.Println("未查询到")
		return nil, err
	}
	if works.Id == 0 {
		fmt.Println("没查询到作品")
		return nil, err
	}
	err = works.CreateMusic(in)
	if err != nil {
		fmt.Println("选择音乐失败")
		return nil, err
	}
	return &video.VideoUploadResp{Greet: "音乐上传成功"}, nil
}

func VideoTopic(in *video.VideoTopicReq) (*video.VideoTopicResp, error) {
	works := mysql.VideoWorks{}
	err := works.GetWorksById(in.WorksId)
	if err != nil {
		fmt.Println("未查询到")
		return nil, err
	}
	if works.Id == 0 {
		fmt.Println("没有该作品")
		return nil, err
	}
	topic := mysql.VideoTopic{}
	err = topic.GetTopicById(in.TopicId)
	if err != nil {
		fmt.Println("未找到话题")
		return nil, err
	}
	if topic.ID == 0 {
		fmt.Println("没有此话题")
		return nil, err
	}
	err = works.GetUpdatyeBtTitle(topic.Title)
	if err != nil {
		fmt.Println("选择话题失败")
		return nil, err
	}
	return &video.VideoTopicResp{Greet: "选择话题成功"}, nil
}

func VideoCreate(in *video.VideoCreateReq) (*video.VideoCreateResp, error) {
	works := mysql.VideoWorks{}
	if works.CheckStatus == "1" {
		fmt.Println("上传是图片")
		err := works.CreateVideo(in)
		if err != nil {
			fmt.Println("作品发布失败")
			return nil, err
		}
	}
	fmt.Println("上传是图片")
	err := works.CreateVideo(in)
	if err != nil {
		fmt.Println("作品发布失败")
		return nil, err
	}
	return &video.VideoCreateResp{Greet: "作品发布成功"}, nil
}

func VideoList(in *video.VideoListReq) (*video.VideoListResp, error) {
	works := mysql.VideoWorks{}
	check, err := works.GetWorksByCheck(in.CheckStatus)
	if err != nil {
		fmt.Println("未展示出")
		return nil, err
	}
	list := []*video.VideoItem{}
	for _, v := range check {
		lists := &video.VideoItem{
			Title:          v.Title,
			Desc:           v.Desc,
			MusicId:        int64(v.MusicId),
			WorkType:       v.WorkType,
			CheckStatus:    v.CheckStatus,
			IpAddress:      v.IpAddress,
			WorkPermission: v.WorkPermission,
			LikeCount:      int64(v.LikeCount),
			CommentCount:   int64(v.CommentCount),
		}
		list = append(list, lists)
	}
	return &video.VideoListResp{List: list}, nil
}

func VideoLikes(in *video.VideoLikesReq) (*video.VideoLikesResp, error) {
	works := mysql.VideoWorks{}
	err := works.GetWorksById(in.Id)
	if err != nil {
		fmt.Println("没有该作品")
		return nil, err
	}
	err = works.UpdateLikes(in.Id, works.LikeCount)
	if err != nil {
		fmt.Println("点赞失败")
		return nil, err
	}
	return &video.VideoLikesResp{Greet: "点赞成功"}, nil
}

func VideoLikej(in *video.VideoLikejReq) (*video.VideoLikejResp, error) {
	works := mysql.VideoWorks{}
	err := works.GetWorksById(in.Id)
	if err != nil {
		fmt.Println("没有该作品")
		return nil, err
	}
	err = works.UpdateLikej(in.Id, works.LikeCount)
	if err != nil {
		fmt.Println("取消赞成功")
		return nil, err
	}
	return &video.VideoLikejResp{Greet: "取消赞成功"}, nil
}
